package com.solome.ui;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.solome.util.FontFactory;

/**
 * 重写，接受中文输入
 * 
 * @author soells
 *
 */
public class SoTextField extends TextField {
	ShapeRenderer shapes = new ShapeRenderer();;
	private int fontSize;
	static private Color focusedFontColor = Color.RED;
	static private Color disabledFontColor = Color.GREEN;
	BitmapFont bitmapFont;

	public SoTextField(CharSequence font, int fontSize, Color color) {
		// TODO Auto-generated constructor stub
		super((String) font, getStyle(font, fontSize, color));
		shapes.setColor(Color.RED);
		this.fontSize = fontSize;
	}

	@Override
	public void draw(Batch batch, float parentAlpha) {
		// TODO Auto-generated method stub
		BitmapFont bf = getBitmapFont();
		if (bitmapFont == null && bf != null) {
			System.out.println(".............draw.............");
			bitmapFont = bf;
			this.getStyle().font.dispose();
			this.getStyle().font = bitmapFont;
		} else {
			return;
		}
		super.draw(batch, parentAlpha);
	}

	private static TextFieldStyle getStyle(CharSequence font, int fontSize,
			Color color) {
		FontFactory.getInstance().init(font.toString(), fontSize);
		TextFieldStyle fieldStyle = new TextFieldStyle();
		fieldStyle.font = new BitmapFont();
		fieldStyle.fontColor = color;
		fieldStyle.messageFontColor = color;
		fieldStyle.focusedFontColor = focusedFontColor;
		fieldStyle.disabledFontColor = disabledFontColor;
		return fieldStyle;
	}

	public BitmapFont getBitmapFont() {
		return FontFactory.getInstance().getFont(fontSize);
	}
}
